define([
	'layer',
	'selectModify',
	'plupload',
	'../../Shop/module/STH_main.js'
], function(layer, selectModify, plupload, main) {

	return {
		imagesAddM: function(con) {
			var _this = this,
				key = con.key,
				order = con.order,
				size = con.prop.size,
				ItemList = con.ItemList,
				listLng = ItemList.length,
				newPanel = main.getPanel("images"),
				newEdit = main.getEdit("images");

			$(newPanel).attr("order", order);
			$(newPanel).attr("key", key);
			$(newEdit).attr("key", key);

			//老数据样式的调整以及数据的更新
//			if(typeof ItemList[0].firLink == "undefined") {
//				if(size == "0" || size == "1") {
//					size = "0";
//					g_data[key].prop.size = "0";
//				}
//				if(size == "2") {
//					size = "1";
//					g_data[key].prop.size = "1";
//				}
//			}

			//用户选中的样式
			$(newEdit).find(".clearfix ul li").eq(size).addClass("active").siblings().removeClass("active");
			$(newPanel).find(".STH_images_panel").attr("class", "STH_images_panel STH_images_style_" + size);

			var newPanelLi = $(newPanel).find("#imagesmb").html(),
				newEditLi = $(newEdit).find("#imagesmb").html();

			//填充之前清空后台和预览容器中的内容		        
			$(newEdit).find("ul.com-list").empty();

			for(var d = 0; d < listLng; d++) { 
				var newobj = {
						"urldata": g_data[key].ItemList[d]["Fsrc"],
						"title": g_data[key].ItemList[d]["Ftitle"],
						"url": g_data[key].ItemList[d].Fsrc,
						"Fname": g_data[key].ItemList[d].Ftypename
				}
 				nELi = main.formatTemplate(newobj, newEditLi);

				$(newPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").append("<a href='" + newobj.urldata + "'><img src='" + newobj.urldata + "' alt='" + newobj.title + "'/></a>");
				$(newPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").attr("rel", "");
				$(newEdit).find("ul.com-list").append(nELi);
				$(newEdit).find("ul.com-list").find("sel_1").eq(d).val(g_data[key].ItemList[d].Ftype);
				$(newEdit).find("ul.com-list").find("sel_2").eq(d).val(g_data[key].ItemList[d].Ftypeid);

			}
			$(newPanel).find(".STH_images_box .STH_images").attr("class", "STH_images STH_iDefault_" + size);

			//延时顺序重定位
			var edLength = $(".hb_box .panel-comp").length;
			if($(".hb_box .panel-comp").length < 1 || order > parseInt($(".hb_box .panel-comp").eq(edLength - 1).attr("order"))) {
				$(".hb_box").append(newPanel);
			} else if(order < parseInt($(".hb_box .panel-comp").eq(0).attr("order"))) {
				$(".hb_box").prepend(newPanel);
			} else {
				for(var r = 0; r < edLength; r++) {
					if(order > parseInt($(".hb_box .panel-comp").eq(r).attr("order")) && order < parseInt($(".hb_box .panel-comp").eq(r + 1).attr("order"))) {
						$(".hb_box .panel-comp").eq(r).after(newPanel);
						break;
					}
				}
			}
			_this.panelImgshow(newPanel, size);
			$(newEdit).css("display", "none");
			$(".edit-panel").append(newEdit);
			_this.imagesActive(newPanel, newEdit, key);
		},
		panelImgshow: function(thisPanel, index) {
			var _this = this;
			$(thisPanel).find(".STH_images_box .STH_images").each(function() {
				var nowindex = $(this).index(),
					htmllen = $(this).html().length;
				$(this).attr("class", "STH_images STH_iDefault_" + index);
				if((index == 0 || index == 1) && nowindex >= 1 && htmllen < 1) {
					$(this).css("display", "none");
				} else if(index == 3 && nowindex > 2 && htmllen < 1) {
					$(this).css("display", "none");
				} else if((index == 4 || index == 5) && nowindex > 3 && htmllen < 1) {
					$(this).css("display", "none");
				} else if(index == 6 && nowindex > 4 && htmllen < 1) {
					$(this).css("display", "none");
				} else {
					$(this).css("display", "block");
				}
			});
			_this.panelImgstyle(thisPanel, index);
		},
		panelImgstyle: function(thisPanel, index) { 
			switch(index) {
				case 0:
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						var htmllen = $(this).html().length,
							_this = this;
						if(htmllen > 0 && $(this).find("img").attr("src") != "") {
							var newimg = new Image();
							newimg.src = $(this).find("img").attr("src");
							newimg.onload = function() {
								$(_this).height($(_this).find("img").height());
							}
						} else {
							$(_this).height(160);
						}
						$(this).css("margin-top", "0px");
					}); 
					break;

				case 1:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						$(this).height(h1);
						$(this).css("margin-top", "0px");
					});
					break;

				case 2:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						$(this).height(h1);
						$(this).css("margin-top", "0px");
					});
					break;

				case 3:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						var index = $(this).index();
						if(((index + 3) % 3) == 0) {
							$(this).height(h1 + 4);
						} else {
							$(this).height(h1 / 2);
						}
						$(this).css("margin-top", "0px");
					});
					break;

				case 4:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						$(this).height(h1);
						$(this).css("margin-top", "0px");
					});
					break;

				case 5:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						var index = $(this).index();
						if(index == 0 || index == 3 || index == 4) {
							$(this).height(h1 / 2);
						} else if(index == 2) {
							$(this).css("margin-top", (h1 / 2 * (-1)) + "px");
							$(this).height(h1);
						} else {
							$(this).height(h1);
						}
					});
					break;

				case 6:
					var h1 = $(thisPanel).find(".STH_images_box .STH_images:eq(0)").width();
					$(thisPanel).find(".STH_images_box .STH_images").each(function() {
						var index = $(this).index();
						if(index == 0) {
							$(this).height(h1);
						} else {
							$(this).height(h1);
						}
						$(this).css("margin-top", "0px");
					});
					break;
			}
			
		},
		imagesActive: function(thisPanel, thisEdit, key) {

			var _this = this,
				upload_index = 0,
				imgloadinit, imgId = "imgupload" + key;
			if(typeof g_data[key].prop.size == "undefined") {
				g_data[key].prop = {
					"size": 0
				};
				g_data[key].ItemList[0] = {
					"Fsrc": "",
					"Fimgid": "",
					"Ftitle": "",
					"Ftypeid": "0",
					"Ftype": "",
					"Fname":"请选择"
				};
				$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").append("<a href=''><img src='' alt=''/></a>");
				$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").attr("rel", "");
			}

			 
			//图片上传操作
			function imgUpload(thisPanel, thisEdit, key) {
				$(thisEdit).find(".com-list-box-choice .com-list-box-choice-pic").unbind("click").click(function() {
					var upload_index = $(thisEdit).find(".com-list-box-choice .com-list-box-choice-pic").index(this);
					 
					main.photoDialog(function(res) {
						var path = res[0].replace(small, middle);
						$eimg = $(thisEdit).find(".com-list .com-list-box-choice:eq(" + upload_index + ")").find(".com-list-box-choice-pic"),
					    $pimg = $(thisPanel).find(".STH_images_box .STH_images:eq(" + upload_index + ")");

                        if ($eimg.find("img").length > 0) {
                            $eimg.find("img").attr("src", path);
                        } else {
                            $eimg.prepend("<img src='" + path + "' >");
                        }

                        if ($pimg.find("img").length > 0) {
                            $pimg.find("img").attr("src",path);
                        } else {
                            $pimg.prepend("<a href=''><img src='" +path+ "' ></a>");
                        }

                        if (g_data[key].ItemList[upload_index]) {
                            g_data[key].ItemList[upload_index].Fsrc = path; 
                        } else {
                            g_data[key].ItemList[upload_index] = {
                                "Fsrc": path 
                            };
                        }
                        _this.panelImgstyle(thisPanel, g_data[key].prop.size);
						
						//改变数据
						g_data[key].ItemList[upload_index].Fsrc = path;

					});
				});
			}

			//属性操作
			function imagesChange(thisPanel, thisEdit, key) {
				//点击选择图片样式
				$(thisEdit).find(".clearfix ul li").unbind("click").click(function() {
					var index = $(this).index();
					if(index == g_data[key].prop.size) return;
					$(this).addClass("active").siblings().removeClass("active");
					$(thisPanel).find(".STH_images_panel").removeClass().addClass("STH_images_panel STH_images_style_" + index);
					g_data[key].prop['size'] = index;
					_this.panelImgshow(thisPanel, index);
					
				});

				//点击按钮添加
				$(thisEdit).find(".clearfix .btn").unbind("click").on("click", function() {
					if($(thisEdit).find(".com-list .choice").length >= 6) {
						layer.alert("最多只能添加6张图片！");
						return;
					} else {
						var dataObject = {
								"urldata": '../../css/skin/default/add_pic.png',
								"title": '',
								"Fname":''
							},
							tempmb = $(thisEdit).find("#imagesmb").html(),
							str = main.formatTemplate(dataObject, tempmb);
						$(thisEdit).find("ul.com-list").append(str);
						$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").append("<a href=''><img src='' alt=''/></a>");
						$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").attr("rel", "");
						_this.panelImgshow(thisPanel, g_data[key].prop.size);
						g_data[key].ItemList.push({
							"Fsrc": "",
							"Ftitle": "",
							"Fid": "0",
							"Ftype": "",
							"Fname":"请选择"
						});
						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						imgDelete(thisPanel, thisEdit, key);
						imgSort(thisPanel, thisEdit, key);
						imagesChange(thisPanel, thisEdit, key);
						initText(thisPanel, thisEdit, key);
					}
				});

				//图片标题操作
				$(thisEdit).find("ul.com-list").unbind("blur").on("blur", ".choice input[name='title']", function() {
					var titleIndex = $(this).parents(".choice").index(),
						titleVal = filterXSS(main.html_encode($(this).val()));
					if(titleVal.length > 30) {
						titleVal = titleVal.slice(0, 30);
					}
					$(this).val(titleVal);
					g_data[key].ItemList[titleIndex].Ftitle = titleVal;
				});

			}

			//一级select
			function selectOne() {  
				$(thisEdit).find('.com-list .sel_1').selectModify({
					onSelectChange: function(e, attrs) {
						var one_val = $(e).val();
						onindex = $(e).parents(".com-list-box-choice").index();

						selectTwo(onindex);
					}
				});
			}

			//二级select函数
			function selectTwo(t) { 
				$(thisEdit).find('.sw_choices .weipage_url').eq(t).val("");
				var item = g_data[key].ItemList[t];
				if(item) {
					item.Ftypeid = 0;
					item.Ftype = 0;
				}
			}
			
		 
			function initText(thisPanel, thisEdit, key){
				var obj =  $(thisEdit).find(".weipage_url");
				$(obj).unbind("click").click(function() {
					var index = $(thisEdit).find(".weipage_url").index(this);
 
					var selText = $(this).parents(".goodsPricePrevious").find("select.sel_1").val();
					var weipage_url = $(this);

					g_data[key].ItemList[index].Ftype = selText;
					if(selText == "1") { //微页面
						main.commonDialog('vstore/tb_page/Sql.html', '选择微页面', 800, 600, function(res) {

							$(weipage_url).val(res.PageName);
							g_data[key].ItemList[index].Ftypeid = res.ID;

						});

					} else if(selText == "2") { //商品分类
						main.typeDialog(function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "3") { //单商品
						main.productDialog('', function(res) {
							$(weipage_url).val(res[0].Fsummary);
							g_data[key].ItemList[index].Ftypeid = res[0].Fid;
						});

					} else if(selText == "4") { //闪购活动
						main.commonDialog('vstore/tb_activity_time/Sql.html', '闪购活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "5") { //团购活动
						main.commonDialog('vstore/tb_activity_group/Sql.html', '团购活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					} else if(selText == "6") { //拼团活动
						main.commonDialog('vstore/tb_activity_group_user/Sql.html', '拼团活动', 800, 600, function(res) {

							$(weipage_url).val(res.Fname);
							g_data[key].ItemList[index].Ftypeid = res.Fid;
						});

					}
				});

			}

			//删除操作
			function imgDelete(thisPanel, thisEdit, key) {
				$(thisEdit).find(".com-list").unbind("click").on("click", ".choice .com-list-box-delete", function() {
					var index = $(this).parents(".choice").index(),
						$this = $(this);
					if($(thisEdit).find(".choice").length <= 1) {
						$(thisEdit).find(".com-list").html($(main.getEdit("images")).find(".com-list").html());

						//商户版本
//						if(sellerVersion != 2 && sellerVersion != 3) {
//							$(thisEdit).find('.com-list .sel_1').eq(0).append("<option value='微页面'>微页面</option><option value='商品分类'>商品分类</option><option value='商品'>商品</option><option value='服务'>服务</option><option value='服务分类'>服务分类</option><option value='服务人员'>服务人员</option><option value='服务人员等级'>服务人员等级</option><option value='外部链接'>外部链接</option>");
//						} else {
//							$(thisEdit).find('.com-list .sel_1').eq(0).append("<option value='微页面'>微页面</option><option value='商品分类'>商品分类</option><option value='商品'>商品</option><option value='外部链接'>外部链接</option>");
//						}

						$(thisPanel).find(".STH_images_box").html($(main.getPanel("images")).find(".STH_images_box").html());
						$(thisPanel).find(".STH_images_box .STH_images").attr("class", "STH_images STH_iDefault_" + g_data[key].prop.size);
						$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").append("<a href=''><img src='' alt=''/></a>");
						$(thisPanel).find(".STH_images_box .STH_images[rel='yuan']:eq(0)").attr("rel", "");
						layer.msg('只有一张图了，内容已清空', {
							"icon": 0
						}, function() {
							g_data[key].ItemList[0].Fsrc = "";
							g_data[key].ItemList[0].Ftitle = ""; 
							g_data[key].ItemList[0].Ftype = "";
							g_data[key].ItemList[0].Ftypeid = "0";  
						});

					} else {
						$this.parents(".choice").remove();
						g_data[key].ItemList.splice(index, 1); 
						for(var i = index; i < 6; i++) {
							var $this = $(thisPanel).find(".STH_images_box .STH_images").eq(i),
								$next = $(thisPanel).find(".STH_images_box .STH_images").eq(i + 1);
							if($next.length > 0 && $next.html().length > 0) {
								$this.html($next.html());
								$this.attr("rel", "");
								$next.html("");
								$next.attr("rel", "yuan");
							} else {
								$this.html("");
								$this.attr("rel", "yuan");
							}
						}
					}
					_this.panelImgshow(thisPanel, g_data[key].prop.size);
					selectOne();
					selectTwo();
					imgUpload(thisPanel, thisEdit, key);
					imgDelete(thisPanel, thisEdit, key);
					imgSort(thisPanel, thisEdit, key);
					imagesChange(thisPanel, thisEdit, key);
				});
			}

			//图片排序操作
			function imgSort(thisPanel, thisEdit, key) {
				//上移
				$(thisEdit).undelegate(".com-list-box-operate .arrow-up", 'click').delegate(".com-list-box-operate .arrow-up", 'click', function() {

					var index = $(thisEdit).find(".com-list-box-operate .arrow-up").index(this);

					if(index > 0) {
						var htmlE = $(this).parents(".choice").prop("outerHTML"),
							htmlP = $(thisPanel).find(".STH_images_box .STH_images:eq(" + index + ")").prop("outerHTML"),
							sel_1val = $(this).parents(".choice").find("select.sel_1").val(),
							sel_2val = $(this).parents(".choice").find("select.sel_2").val();
						$(thisEdit).find(".com-list li.choice").eq(index - 1).before(htmlE);
						$(thisEdit).find(".com-list li.choice").eq(index + 1).remove();
						$(thisPanel).find(".STH_images_box .STH_images:eq(" + (index - 1) + ")").before(htmlP);
						$(thisPanel).find(".STH_images_box .STH_images:eq(" + (index + 1) + ")").remove();
						$(thisEdit).find(".com-list li.choice").eq(index - 1).find("select.sel_1").val(sel_1val);
						$(thisEdit).find(".com-list li.choice").eq(index - 1).find("select.sel_2").val(sel_2val);
						main.exchange(g_data[key].ItemList, (index - 1), index);
						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						imgDelete(thisPanel, thisEdit, key);
						imgSort(thisPanel, thisEdit, key);
						imagesChange(thisPanel, thisEdit, key);

					}
				});

				//下移
				$(thisEdit).undelegate(".com-list-box-operate .arrow-down", 'click').delegate(".com-list-box-operate .arrow-down", 'click', function() {
					var index = $(thisEdit).find(".com-list-box-operate .arrow-down").index(this),
						lilen = $(thisEdit).find(".com-list-box-operate .arrow-down").length;

					if(index < (lilen - 1)) {
						var htmlE = $(this).parents(".choice").prop("outerHTML"),
							htmlP = $(thisPanel).find(".STH_images_box .STH_images:eq(" + index + ")").prop("outerHTML"),
							sel_1val = $(this).parents(".choice").find("select.sel_1").val(),
							sel_2val = $(this).parents(".choice").find("select.sel_2").val();

						$(thisEdit).find(".com-list li.choice").eq(index + 1).after(htmlE);
						$(thisEdit).find(".com-list li.choice").eq(index).remove();
						$(thisPanel).find(".STH_images_box .STH_images:eq(" + (index + 1) + ")").after(htmlP);
						$(thisPanel).find(".STH_images_box .STH_images:eq(" + index + ")").remove();
						$(thisEdit).find(".com-list li.choice").eq(index + 1).find("select.sel_1").val(sel_1val);
						$(thisEdit).find(".com-list li.choice").eq(index + 1).find("select.sel_2").val(sel_2val);
						main.exchange(g_data[key].ItemList, index, (index + 1));

						selectOne();
						selectTwo();
						imgUpload(thisPanel, thisEdit, key);
						imgDelete(thisPanel, thisEdit, key);
						imgSort(thisPanel, thisEdit, key);
						imagesChange(thisPanel, thisEdit, key);
					}
				});

			}

			selectOne();
			selectTwo();
			imgUpload(thisPanel, thisEdit, key);
			imgDelete(thisPanel, thisEdit, key);
			imgSort(thisPanel, thisEdit, key);
			imagesChange(thisPanel, thisEdit, key);
			initText(thisPanel, thisEdit, key);
			
			//调用主模板JS
			main.saveClick();
			main.moduleActive();
			main.dragEdit();
		}

	}

})